package org.jeecg.common.es.config;
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

import org.apache.http.HttpHost;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder.RequestConfigCallback;
import org.elasticsearch.client.RestHighLevelClient;
import org.jeecg.common.es.template.CssdElasticsearchRestTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Configuration
public class RestClientConfig {
    static final String COLON = ":";
    static final String COMMA = ",";
    @Value("${elastic.endpoints}")
    private String endpoints;
    @Value("${elastic.connection-timeout}")
    private Integer connectTimeOutMillis;
    @Value("${elastic.socket-timeout}")
    private Integer socketTimeoutMillis;
    @Value("${elastic.connection-request-timeout}")
    private Integer connectionRequestTimeoutMillis;
    @Value("${elastic.max-connection-per-route}")
    private Integer maxConnPerRoute;
    @Value("${elastic.max-connection-total}")
    private Integer maxConnTotal;
    @Value("${elastic.scheme}")
    private String scheme;
    @Value("${elastic.username:admin}")
    private String username;
    @Value("${elastic.password:admin}")
    private String password;

    public RestClientConfig() {}

    @Bean
    public RestHighLevelClient elasticsearchClient() {
        String[] split = this.endpoints.split(",");
        HttpHost[] httpHosts = new HttpHost[split.length];
        HttpHost[] collect = (HttpHost[])((List)Arrays.stream(split).map((s) -> {
            String[] split1 = s.split(":");
            HttpHost httpHost = new HttpHost(split1[0], Integer.parseInt(split1[1]), this.scheme);
            return httpHost;
        }).collect(Collectors.toList())).toArray(httpHosts);
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        // credentialsProvider.setCredentials(AuthScope.ANY,
        // new UsernamePasswordCredentials(this.username, this.password));
        return new RestHighLevelClient(RestClient.builder(collect)
            .setRequestConfigCallback(this.requestConfigCallback()).setHttpClientConfigCallback((httpClientBuilder) -> {
                httpClientBuilder.setMaxConnTotal(this.maxConnTotal);
                httpClientBuilder.setMaxConnPerRoute(this.maxConnPerRoute);
                httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                httpClientBuilder.setKeepAliveStrategy((response, context) -> {
                    return 900000L;
                });
                return httpClientBuilder;
            }));
    }

    private RequestConfigCallback requestConfigCallback() {
        return (builder) -> {
            builder.setConnectTimeout(this.connectTimeOutMillis).setSocketTimeout(this.socketTimeoutMillis)
                .setConnectionRequestTimeout(this.connectionRequestTimeoutMillis).setMaxRedirects(300000);
            return builder;
        };
    }

    @Bean(name = {"elasticsearchTemplate"})
    public CssdElasticsearchRestTemplate elasticsearchTemplate() {
        return new CssdElasticsearchRestTemplate(this.elasticsearchClient());
    }
}
